package com.kuizii.generator.util.generator;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * <p>
 * 生成所有当前项目的父类
 * </p>
 *
 * @author nuanyang
 * Email
 * created by 2018/6/20
 */
public class GeneratorParentUtils {

    /**
     * 获取模板
     *
     * @return
     */
    private static List<String> getTemplates() {
        List<String> templates = new ArrayList<>();
        templates.add("templates/parent/pom.xml.vm");
        templates.add("templates/bin/1.release-prepare.bat.vm");
        templates.add("templates/bin/2.release-perform.bat.vm");
        templates.add("templates/bin/branch.bat.vm");
        templates.add("templates/bin/deploy.bat.vm");
        templates.add("templates/bin/release-clean.bat.vm");
        templates.add("templates/bin/release-rollback.bat.vm");
        return templates;
    }

    /**
     * 自动生成代码
     *
     * @param zip
     */
    public static void generatorCode(ZipOutputStream zip) {
        Configuration config = GeneratorConfig.getConfig();
        //设置配置
        Map<String, String> map = new HashMap<>();
        map.put("artifactId", GeneratorConfig.getModuleParent() + GeneratorConfig.getModuleName());
        map.put("coreArtifactId", GeneratorConfig.getModuleParent() + GeneratorConfig.getModulePrefix() + GeneratorConfig.MODULE_CORE);
        map.put("apiArtifactId", GeneratorConfig.getModuleParent() + GeneratorConfig.getModulePrefix() + GeneratorConfig.MODULE_API);
        map.put("bizArtifactId", GeneratorConfig.getModuleParent() + GeneratorConfig.getModulePrefix() + GeneratorConfig.MODULE_BIZ);
        map.put("starterArtifactId", GeneratorConfig.getModuleParent() + GeneratorConfig.getModulePrefix() + GeneratorConfig.MODULE_STARTER);
        map.put("webApiArtifactId", GeneratorConfig.getModuleParent() + GeneratorConfig.getModulePrefix() + GeneratorConfig.MODULE_WEB_API);
        map.put("parentArtifactId", GeneratorConfig.getModuleParent() + GeneratorConfig.getModuleName());
        map.put("moduleName", GeneratorConfig.getModuleName().replace("-", "."));
        map.put("rpc-type", config.getString("rpc-type"));
        map.put("package", config.getString("package"));
        map.put("version", config.getString("version"));
        map.put("platformType", config.getString("platformType"));
        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);

        VelocityContext context = new VelocityContext(map);
        for (String template :
                getTemplates()) {
            StringWriter writer = new StringWriter();
            Template ftl = Velocity.getTemplate(template, "UTF-8");
            ftl.merge(context, writer);
            try {
                zip.putNextEntry(new ZipEntry(Objects.requireNonNull(getFileName(template))));
                IOUtils.write(writer.toString(), zip, "UTF-8");
                IOUtils.closeQuietly(writer);
                zip.closeEntry();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String getFileName(String template) {
        /*
        templates.add("templates/parent/pom.xml.vm");
        templates.add("templates/bin/1.release-prepare.bat.vm");
        templates.add("templates/bin/2.release-perform.bat.vm");
        templates.add("templates/bin/branch.bat.vm");
        templates.add("templates/bin/deploy.bat.vm");
        templates.add("templates/bin/release-clean.bat.vm");
        templates.add("templates/bin/release-rollback.bat.vm");
         */
        if (template.contains("pom.xml.vm")) {
            return GeneratorConfig.getProjectParentName() + "pom.xml";
        }
        if (template.contains("1.release-prepare.bat.vm")) {
            return GeneratorConfig.getProjectParentName() + "bin" + File.separator + "1.release-prepare.bat";
        }
        if (template.contains("2.release-perform.bat.vm")) {
            return GeneratorConfig.getProjectParentName() + "bin" + File.separator + "2.release-perform.bat";
        }
        if (template.contains("branch.bat.vm")) {
            return GeneratorConfig.getProjectParentName() + "bin" + File.separator + "branch.bat";
        }
        if (template.contains("deploy.bat.vm")) {
            return GeneratorConfig.getProjectParentName() + "bin" + File.separator + "deploy.bat";
        }
        if (template.contains("release-clean.bat.vm")) {
            return GeneratorConfig.getProjectParentName() + "bin" + File.separator + "release-clean.bat";
        }
        if (template.contains("release-rollback.bat.vm")) {
            return GeneratorConfig.getProjectParentName() + "bin" + File.separator + "release-rollback.bat";
        }
        return null;
    }
}
